import { getLessonList, getAllChannelList } from '@/api/channel'
export default {
  namespaced: true,
  state: {
    lessonList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.lessonList = payload
    },
    getAllChannelList(state, payload) {
      state.allChannelList = payload
    },
    addChannel(state, payload) {
      state.lessonList.push(payload)
    },
    delChannel(state, payload) {
      state.lessonList = state.lessonList.filter(item => item.id !== payload.id)
    }
  },
  actions: {
    async getLessonList(context) {
      const res = await getLessonList()
      console.log(res)
      context.commit('setChannelList', res.data.channels)
    },
    async getAllChannelList(context) {
      const res = await getAllChannelList()
      console.log(res)
      context.commit('getAllChannelList', res.data.channels)
    }
  },
  getters: {
    optionalChannels: state => {
      // 对 allChannels 中的元素进行过滤, 只保留那些没有在 channels 中出现的元素
      return state.allChannelList.filter(channel => {
        return (
          state.lessonList.findIndex(item => +item.id === +channel.id) === -1
        )
      })
    }
  }
}
